package com.greenant.store.utils;

import java.util.Stack;

import android.app.Activity;
import android.content.Context;

public class AppExitManager
{

    private static Stack<Activity> mActivityStack;
    private static AppExitManager mAppExitManager;

    private AppExitManager()
    {
        if (mActivityStack == null)
        {
            mActivityStack = new Stack<Activity>();
        }
    }

    /**
     * 单一实例
     */
    public static AppExitManager getInstance()
    {
        if (mAppExitManager == null)
        {
            mAppExitManager = new AppExitManager();
        }
        return mAppExitManager;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity)
    {
        mActivityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity()
    {
        Activity activity = mActivityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity()
    {
        Activity activity = mActivityStack.lastElement();
        if (activity != null)
        {
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity)
    {
        if (activity != null)
        {
            mActivityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls)
    {
        for (Activity activity : mActivityStack)
        {
            if (activity.getClass().equals(cls))
            {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity()
    {
        
        
        for (int i = 0, size = mActivityStack.size(); i < size; i++)
        {
            if (null != mActivityStack.get(i))
            {
                mActivityStack.get(i).finish();
            }
        }
        mActivityStack.clear();
        // for (Activity activity : mActivityStack) {
        // finishActivity(activity);
        // }
    }

    /**
     * 清空stack
     */
    public void clearStack()
    {
        mActivityStack.clear();
    }

    /**
     * 退出应用程序
     */
    @SuppressWarnings("deprecation")
    public void appExit(Context context)
    {
        try
        {
//            WebServer.getInstance().stopThread();
            finishAllActivity();
            
            //清除掉image的缓存
            
            
            //停止服务器线程
            //WebServer.getInstance().stopThread();
            /* android 2.2以下杀进程方法： */
            // ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            // activityMgr.restartPackage(context.getPackageName());
            /* android 2.2以上杀进程方法: */
//            android.os.Process.killProcess(android.os.Process.myPid());
//            System.exit(0);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
